package com.wude.guild.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.mc.developmentkit.bean.GetPaidInfo;
import com.mc.developmentkit.callback.AliPayListener;
import com.mc.developmentkit.pay.MCThirdPartyPay;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.R;
import com.wude.guild.servercore.app.CubeFragment;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;


/**
 * 平台币充值
 * Created by Administrator on 2017/3/8.
 */

public class PayPtbFragment extends CubeFragment implements View.OnClickListener, AliPayListener {

    private TextView zhanghu;
    private EditText jine;
    private TextView ptb;
    private CheckBox zfb;
    private CheckBox wx;
    private TextView queren;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("PayPtbFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("PayPtbFragment");
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_pay_ptb, null);
        initview(inflate);
        UserInfo loginUser = Utils.getLoginUser();
        zhanghu.setText(loginUser.account);
        return inflate;
    }

    private void initview(View v) {
        zhanghu = v.findViewById(R.id.zhanghu);
        jine = v.findViewById(R.id.jine);
        ptb = v.findViewById(R.id.ptb);
        queren = v.findViewById(R.id.queren);
        zfb = v.findViewById(R.id.zfb);
        wx = v.findViewById(R.id.wx);
        queren.setOnClickListener(this);
        zfb.setChecked(true);
        zfb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    wx.setChecked(false);
                }
            }
        });
        wx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    zfb.setChecked(false);
                }
            }
        });

        jine.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                try {
                    Double s = Double.parseDouble(editable.toString());
                    if (s != 0) {
                        ptb.setText(s * 1 + "");       //如果折扣信息为空 默认该游戏不打折 实付金额为金额*1
                    } else {
                        ptb.setText("");
                    }
                } catch (Exception e) {
                    Log.e("afterTextChanged异常", e.toString());
                    ptb.setText("");
                }

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.queren:
                Pay();
                break;
        }
    }

    /**
     * 点击确认按钮支付
     */
    private void Pay() {
        String jine1 = jine.getText().toString();
        if (jine1.equals("")) {
            ToastUtil.showToast("请输入充值金额");
            return;
        }
        if (!jin(jine1)) {
            ToastUtil.showToast("充值金额不正确");
            return;
        }
        if (!zfb.isChecked() && !wx.isChecked()) {
            ToastUtil.showToast("请选择一种支付方式");
            return;
        }
        UserInfo loginUser = Utils.getLoginUser();
        GetPaidInfo getPaidInfo = new GetPaidInfo();
//        getPaidInfo.pay_amount = jine1;                 //应付金额
        getPaidInfo.pay_amount = Utils.getBase64String(jine1.getBytes(), 0);
        getPaidInfo.good_type = 1;                      //1平台币   2绑币
        getPaidInfo.user_sign = loginUser.token;        //用户token
        getPaidInfo.promote_id = "1";                   //推广员id
        if (zfb.isChecked()) {
            getPaidInfo.payType = "1";                  //支付宝
            ZFB(getPaidInfo);
        } else if (wx.isChecked()) {
            getPaidInfo.payType = "2";                  //微信
            WX(getPaidInfo);
        }
    }

    public boolean jin(String a) {
        String substring = a.substring(0, 1);
        int i = Integer.parseInt(substring);
        if (i >= 1) {
            return true;
        }
        return false;
    }

    /**
     * 微信支付
     */
    private void WX(GetPaidInfo info) {
        MCThirdPartyPay.getInstance().wxPay(getActivity(), HttpCom.getPayUrl(), info);
    }

    /**
     * 支付宝支付
     */
    private void ZFB(GetPaidInfo info) {
        MCThirdPartyPay.getInstance().aliPay(getActivity(), HttpCom.getPayUrl(), info, this);
    }

    @Override
    public void zfbPayResult(String s) {
        Log.e("支付宝返回", s);
    }
}
